home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1184_missing_gdb_variable.rtf < prev    next >
Text File  |  1995-06-12  |  5KB  |  75 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f1\fmodern Courier;\f2\fmodern Ohlfs;}
  2. \paperw11680
  3. \paperh8100
  4. \margl120
  5. \margr120
  6. {\colortbl;\red0\green0\blue0;\red84\green84\blue84;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\b0\i0\ulnone\fs28\fc1\cf1 Q:  The gdb debugger refuses to display the value of my variable “x.”  Following is the source code and the gdb output:\
  8.  
  9. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\f1\fs24\fc1\cf1 \
  10.  
  11. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc1\cf1     - convert:sender \{\
  12.             char    *fName;\
  13.  \
  14.             foo = NO;\
  15.             [self enableConvert];\
  16.             fName = [myText stringValue];\
  17.  
  18. \b         <--- break here\
  19.  
  20. \pard\tx560\tx1120\tx1920\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\b0\fc1\cf1 \
  21.  
  22. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc1\cf1     (gdb) print fName\
  23.     No symbol "fName" in current context.\
  24.  
  25. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\f0\fs28\fc1\cf1     \
  26.  
  27. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc1\cf1 A:  When code is compiled with the -O flag, the compiler can take liberties to optimize away certain source code variables.  This is accomplished by careful juggling of registers and order of expression evaluation.  For example, stack variables which never have their address taken and are used only across a very few instructions can “disappear” without a trace.   Try compiling your code without optimization and see if this eliminates some of these surprises.  The standard “make <appname>” rules do invoke optimization as shown by the -O flag:\
  28.  
  29. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\f1\fs24\fc1\cf1 \
  30.  
  31. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc1\cf1     myhost> cc -g 
  32. \b -O
  33. \b0  -Wall  -c QCombo.m -o obj/QCombo.o\
  34.  
  35. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\f0\fs28\fc1\cf1 \
  36.  
  37. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc1\cf1 The standard “make debug” rules do not:\
  38.  
  39. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\fc1\cf1 \
  40.  
  41. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f1\fs24\fc1\cf1     myhost> cc -g -Wall -DDEBUG  -c QCombo.m -o debug_obj/QCombo.o\
  42.  
  43. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\f0\fs28\fc1\cf1 \
  44.  
  45. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc1\cf1 The “Debug” button in ProjectBuilder does not assume “make debug.”  You have to add “debug” to the Args text field to pass it as an argument to the make process.\
  46.     \
  47. The man page for 
  48. \b cc
  49. \b0  contains more detail over how one can control compiler optimizations; a small section is repeated here:\
  50.  
  51. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\fc1\cf1 \
  52.  
  53. \pard\tx1540\tx1800\tx2240\tx2800\tx3360\tx3920\tx4480\tx4580\tx5040\tx5600\f1\fs24\fi-520\li1560\fc1\cf1 -O    Optimize.  Optimizing compilation takes somewhat more time, and a lot more memory for a large function.\
  54. \
  55.     Without -O, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results.  With -O, the compiler tries to reduce code size and execution time.  Some of the -f options described below turn specific kinds of optimization on or off.\
  56.  
  57. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\f0\fs28\fc1\cf1 \
  58.  
  59. \pard\tx1540\tx1800\tx2240\tx2800\tx3360\tx3920\tx4480\tx4580\tx5040\tx5600\f1\fs24\fi-520\li1560\fc0\cf0 -g    Produce debugging information in GDB format.\
  60.     \
  61.     Unlike most other C compilers, GNU CC allows you to use -g with -O.  The shortcuts taken by optimized code may occasionally produce surprising results:  Some variables you declared may not exist at all; flow of control may briefly move where you didn't expect it; some statements may not be executed because they compute constant results or their values were already at hand; some statements may execute in different places because they were moved out of loops.  Nevertheless, this makes it possible to debug optimized output if necessary.\
  62.  
  63. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\f0\fs28\fc1\cf1 \
  64.  
  65. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc1\cf1 The fastest way to pursue one missing variable would be to recompile the troublesome source file from the shell, without the -O flag.  Then relink, and debug the new executable.  \
  66.  
  67. \pard\tx380\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\fi-380\li380\fc1\cf1     
  68. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\fc1\cf1 \
  69.  
  70. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0\cf0 QA843\
  71. \
  72. Valid for 1.0, 2.0, 3.0\
  73. \
  74.  
  75.